﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using Sunboxsoft.ASMS.Client.UI.Properties;
namespace Sunboxsoft.ASMS.Client.UI.Package
{
    public partial class PackagePurchaseBillListView : UserControl, IView
    {
        #region fields
        Promise _promise = new Promise();
        WebRef.VCustomer _customer = null;
        private int _pageIndex;
        private int _pageCount;
        //财务/收银进入标示
        private string flag = string.Empty;
        #endregion

        public PackagePurchaseBillListView()
        {
            InitializeComponent();
            this.GCData.EmbeddedNavigator.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(EmbeddedNavigator_ButtonClick);
        }

        public PackagePurchaseBillListView(string flag)
            : this()
        {
            this.flag = flag;
        }

        #region IView 成员
        public void RegisterAction(ActionNodeCollection collection)
        {
            //collection.AddAction(new ActionNode("view", Resources.isView, "查看", null))
            collection.AddAction(new ActionNode("purchase", Resources.isPackage, "购买套餐", OnPurchaseActionClick))
              .AddAction(new ActionNode("checkedout", Resources.isSettling, "收银", OnCheckedOutActionClick))
              .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", OnRefreshActionClick));
        }
        public void Resume(ViewResumeArgs args)
        {
            if (args.ClosedArgs.Result)
            {
                if (args.ClosedArgs.View is Pickers.VCustomerAccuratelyPicker)
                {
                    _customer = args.ClosedArgs.Data as WebRef.VCustomer;
                    this.ctlBtnEditCustomer.Text = _customer.Name;
                }
                else
                {
                    LoadPackagePurchaseBillPagedListData();
                }
            }
        }
        public void Active(ViewActiveArgs args)
        {
            _promise
                .Then(() =>
                {
                    this.BeginInvoke(new Action(() =>
                    {
                        this.ctlLookUpEditStatus.BindIntCodeTextItem(Domain.Constant.PackagePurchaseBillStatus.Enumerate(), null, true);
                        _promise.Resolve();
                    }));
                }).Then(() =>
                {
                    this.BeginInvoke(new Action(() =>
                    {
                        //从财务收银界面进入隐藏状态选项,且只显示未支付的套餐购买单
                        if (!string.IsNullOrEmpty(flag))
                        {
                            this.lblState.Visible = false;
                            this.ctlLookUpEditStatus.Visible = false;
                            this.ctlLookUpEditStatus.EditValue = Domain.Constant.PackagePurchaseBillStatus.Enabled;
                        }
                        _promise.Resolve();
                    }));
                })
                .Then(() =>
                {
                    this.ctlSearchLookUpEditSalesManager.BindEStaff(_promise, false, true);
                })
                .Then(LoadPackagePurchaseBillPagedListData)
                .Run(null);
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("套餐购买列表");
        }
        #endregion

        #region methods
        /// <summary>
        /// 加载套餐购买列表
        /// </summary>
        private void LoadPackagePurchaseBillPagedListData()
        {
            Guid? customerId = null, salesManagerId = null;
            if (_customer != null)
            {
                customerId = _customer.Id;
            }
            WebRef.EStaff salesMan = this.ctlSearchLookUpEditSalesManager.GetSelectedValue<WebRef.EStaff>();
            if (salesMan != null)
            {
                salesManagerId = salesMan.Id;
            }
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.VPackagePurchaseBill> result = new Service.QueryService().QueryPackagePurchaseBill(
                    this.txtQryCondition.Text.Trim(),
                    ModuleContext.Current.BusinessId,
                    ModuleContext.Current.StationId,
                    customerId,
                    salesManagerId,
                  LookUpEditUtil.GetIntCode(this.ctlLookUpEditStatus), 0, Domain.Constant.PageLimitAll);
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        this.GCData.DataSource = null;
                        this.GVData.FocusedRowHandle = -1;
                        this.GCData.DataSource = result.Data.Items;
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Resolve();
                    }
                }));
            });
        }
        #endregion

        #region actions
        /// <summary>
        /// 购买套餐
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnPurchaseActionClick(object sender, object e)
        {
            UIContext.Current.ViewManager.PopupView(new Create.CreatePackagePurchaseBillView(), new PopupViewArgs("客户套餐购买单"));
        }
        /// <summary>
        /// 收银
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnCheckedOutActionClick(object sender, object e)
        {
            try
            {
                if (this.GVData.FocusedRowHandle > -1)
                {
                    WebRef.VPackagePurchaseBill item = this.GVData.GetRow(this.GVData.FocusedRowHandle) as WebRef.VPackagePurchaseBill;
                    UIContext.Current.ViewManager.PopupView(new Package.CheckoutPackagePurchaseBillView(item.Id), new PopupViewArgs("客户购买套餐收银"));
                }
            }
            catch (Exception ex)
            {
                UIContext.Current.ViewManager.ShowError(ex.Message);
            }
        }
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnRefreshActionClick(object sender, object e)
        {
            _pageIndex = 0;
            LoadPackagePurchaseBillPagedListData();
        }
        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            e.UpdatePageIndex(_pageCount, ref _pageIndex);
            LoadPackagePurchaseBillPagedListData();
        }
        /// <summary>
        /// 根据套餐购买单显示对应按钮状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GVData_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                if (this.GVData.FocusedRowHandle > -1)
                {
                    WebRef.VPackagePurchaseBill entity = GVData.GetRow(GVData.FocusedRowHandle) as WebRef.VPackagePurchaseBill;
                    switch (entity.Status)
                    {
                        case Domain.Constant.PackagePurchaseBillStatus.Enabled:
                            this.ShowAction("checkedout", true);
                            break;
                        default:
                            this.ShowAction("checkedout", false);
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                UIContext.Current.ViewManager.ShowError(ex.Message);
            }
        }

        /// <summary>
        /// 选择客户&清除客户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnEditCustomer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.Equals("clear"))
            {
                this.ctlBtnEditCustomer.Text = "";
                _customer = null;
            }
            else
            {
                UIContext.Current.ViewManager.PopupView(new Pickers.VCustomerAccuratelyPicker(), new PopupViewArgs("选择客户"));
            }
        }
        #endregion
    }
}
